package com.xhtt.modules.sys.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhtt.modules.sys.dao.SysUserDeptDao;
import com.xhtt.modules.sys.entity.SysDeptEntity;
import com.xhtt.modules.sys.entity.SysUserDeptEntity;
import com.xhtt.modules.sys.entity.SysUserEntity;
import com.xhtt.modules.sys.service.SysDeptService;
import com.xhtt.modules.sys.service.SysUserDeptService;
import com.xhtt.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * 用户与角色对应关系
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年9月18日 上午9:45:48
 */
@Service("sysUserDeptService")
public class SysUserDeptServiceImpl extends ServiceImpl<SysUserDeptDao, SysUserDeptEntity> implements SysUserDeptService {

    @Autowired
    private SysDeptService sysDeptService;

    @Autowired
    private SysUserService sysUserService;

    @Override
    @Transactional
    public boolean saveOrUpdate(List<Long> userIdList, List<Long> deptIdList) {
        if (deptIdList == null) {
            return false;
        }
        this.remove(new QueryWrapper<SysUserDeptEntity>().in(SysUserDeptEntity.USERID, userIdList));
        if (deptIdList.size() == 0) {
            return false;
        } else {
            //保存用户与部门关系
            List<SysUserDeptEntity> list = new ArrayList<>(deptIdList.size());
            for (Long userId : userIdList) {
                for (Long roleId : deptIdList) {
                    SysUserDeptEntity sysUserDeptEntity = new SysUserDeptEntity();
                    sysUserDeptEntity.setUserId(userId);
                    sysUserDeptEntity.setDeptId(roleId);

                    list.add(sysUserDeptEntity);
                }
            }

            this.saveBatch(list);
        }
        return true;
    }

    @Override
    public List<Long> queryRoleIdList(Long userId) {
        return null;
    }

    @Override
    public SysDeptEntity getDeptIsGroupByUserId(Integer userId) {

        SysUserEntity sysUserEntity = sysUserService.getById(userId);

        if (sysUserEntity != null) {
            Integer deptId = sysUserEntity.getTeamId();
            // 获取类型为组的集合
            SysDeptEntity sysDeptEntity = sysDeptService
                    .getOne(new LambdaQueryWrapper<SysDeptEntity>()
//                            .eq(SysDeptEntity::getType, 2)
                            .eq(SysDeptEntity::getDeptId, deptId));
            return sysDeptEntity;
        }
        return null;
    }

    @Override
    public int deleteBatch(Long[] roleIds) {
        return 0;
    }

    @Override
    public SysDeptEntity getDeptName(Long userId) {
        return baseMapper.getDeptInfo(userId);
    }

    @Override
    public List<SysUserEntity> getUsersByDept(Integer deptId) {
        return baseMapper.getUsersByDept(deptId);
    }


}
